package com.lsz.crm.controller;

import com.lsz.crm.annotation.RequiredPermission;
import com.lsz.crm.base.BaseController;
import com.lsz.crm.base.ResultInfo;
import com.lsz.crm.enums.StateStatus;
import com.lsz.crm.pojo.SaleChance;
import com.lsz.crm.query.SaleChanceQuery;
import com.lsz.crm.service.SaleChanceService;
import com.lsz.crm.utils.CookieUtil;
import com.lsz.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/10 15:47
 * @Version 1.0
 */
@Controller
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController{

    @Autowired
    private SaleChanceService saleChanceService;

    /**
     * 跳转营销机会管理页 1010
     * @return
     */
    @RequiredPermission(code = "1010")
    @RequestMapping("index")
    public String index(){

        return "SaleChance/saleChance";
    }

    /**
     * 营销机会视图列表展示 101001
     * @param saleChanceQuery
     * @return
     */
    @RequiredPermission(code = "101001")
    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,
                                                      Integer flag,HttpServletRequest request){
        // flag不空且值为1，表明查询的是客户开发计划
        if(flag != null && flag == 1){
            // 设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            // 设置指派人(当前登录用户id)
            saleChanceQuery.setAssignMan(LoginUserUtil.releaseUserIdFromCookie(request) + "");
        }
        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    /**
     * 添加营销机会  101002
     * @param saleChance
     * @return
     */
    @RequiredPermission(code = "101002")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){
        // 从 cookie 中获取用户名
        String userName = CookieUtil.getCookieValue(request,"userName");
        // 设置用户名到营销机会对象
        saleChance.setCreateMan(userName);
        // 调用 service
        saleChanceService.addSaleChance(saleChance);
        // 返回 ResultInfo 对象
        return success("营销机会添加成功！");
    }

    /**
     * 删除营销机会 101003
     * @param ids
     * @return
     */
    @RequiredPermission(code = "101003")
    @PostMapping("del")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids){
        // 调用 service
        saleChanceService.deleteSaleChance(ids);
        // 返回 ResultInfo 对象
        return success("营销机会删除成功！");
    }

    /**
     * 更新营销机会 101004
     * @param saleChance
     * @return
     */
    @RequiredPermission(code = "101004")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){
        // 调用 service
        saleChanceService.updateSaleChance(saleChance);
        // 返回 ResultInfo 对象
        return success("营销机会更新成功！");
    }

    /**
     * 进入添加/修改营销机会页面
     * @return
     */
    @RequestMapping("toSaleChancePage")
    public String toSaleChancePage(Integer id,HttpServletRequest request){
        if(id != null){
            //表明是修改操作
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(id);
            // 见数据设置到请求域中
            request.setAttribute("saleChance",saleChance);
        }
        return "SaleChance/addAndUpdate";
    }


}
